package org.zasadnyy.redlions.players.client.ui;

import org.zasadnyy.redlions.players.client.ds.PlayersDataSource;
import org.zasadnyy.redlions.players.shared.model.PlayerItem;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.BkgndRepeat;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class EditPlayersLayout extends VLayout {

	private PlayersDataSource datasource;
	
	public EditPlayersLayout() {
		super();
		this.datasource = new PlayersDataSource();
		initLayout();
	}

	public interface PlayersChangedHandelr {
		void onPlayerChange();
	}

	private static class DefaultDSCallback implements DSCallback {

		private PlayersChangedHandelr changeHandler;

		public DefaultDSCallback(PlayersChangedHandelr changeHandler) {
			this.changeHandler = changeHandler;
		}

		@Override
		public void execute(DSResponse response, Object rawData,
				DSRequest request) {
			changeHandler.onPlayerChange();
		}

	}

	private void initLayout() {
		
		final HLayout mainLayout = createMainLayout();
		final DynamicForm editForm = createEditForm(datasource);
		final ListGrid playerGrid = createPlayersGrid(datasource, editForm);
		final VLayout rightPanel = createRightPanel();
		final HLayout topButtons = createButtonsLayout(editForm, playerGrid);
		final HLayout bottomButtons = createButtonsLayout(editForm, playerGrid);

		setupHomeLayout();
		mainLayout.addMember(playerGrid);
		mainLayout.addMember(rightPanel);
		rightPanel.addMember(topButtons);
		rightPanel.addMember(editForm);
		rightPanel.addMember(bottomButtons);
		
		editForm.fetchData();

		addMember(mainLayout);

	}

	private void setupHomeLayout() {
		setBackgroundImage("img/p.png");
		setBackgroundRepeat(BkgndRepeat.REPEAT);
		setWidth100();
		setHeight100();
		setPadding(10);
	}

	private ListGrid createPlayersGrid(PlayersDataSource ds,
			final DynamicForm editForm) {
		final ListGrid playerGrid = new ListGrid();
		playerGrid.setWidth(180);
		playerGrid.setHeight100();
		playerGrid.setDataSource(ds);
		playerGrid.setAutoFetchData(true);
		

		ListGridField nameField = new ListGridField(PlayerItem.NAME, "Ім'я");
		ListGridField surnamefield = new ListGridField(PlayerItem.SURNAME,
				"Прізвище");
		playerGrid.setFields(nameField, surnamefield);
		playerGrid.addSelectionChangedHandler(new SelectionChangedHandler() {

			@Override
			public void onSelectionChanged(SelectionEvent event) {
				Record record = event.getRecord();
				if (record != null) {
					Criteria criteria = new Criteria(PlayerItem.ID, record
							.getAttribute(PlayerItem.ID));
					 editForm.fetchData(criteria);
				}
			}
		});
		return playerGrid;
	}

	private DynamicForm createEditForm(PlayersDataSource ds) {
		DynamicForm editForm = new DynamicForm();
		editForm.setDataSource(ds);
		editForm.setWidth("*");
		editForm.setHeight100();
		editForm.setNumCols(2);
		return editForm;
	}

	private HLayout createButtonsLayout(final DynamicForm editForm,
			final ListGrid playerGrid) {
		HLayout buttons = new HLayout(10);
		buttons.setWidth100();
		buttons.setAlign(Alignment.RIGHT);
		buttons.setPadding(10);

		final DefaultDSCallback defaulCallback = new DefaultDSCallback(
				new PlayersChangedHandelr() {

					@Override
					public void onPlayerChange() {
						playerGrid.fetchData();
					}
				});

		Button saveBtn = new Button("Зберегти");
		saveBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				editForm.saveData(defaulCallback, null);
			}

		});
		buttons.addMember(saveBtn);

		Button deleButton = new Button("Видалити");
		deleButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				ListGridRecord selectedRecord = playerGrid.getSelectedRecord();
				SC.ask("Ви справді бажаєте видалити "
						+ selectedRecord.getAttribute(PlayerItem.NAME) + " ?",
						new BooleanCallback() {

							@Override
							public void execute(Boolean value) {
								if (value) {
									if (playerGrid.getSelection() != null) {
										playerGrid.removeSelectedData(
												defaulCallback, null);
									}
								}
							}
						});
			}
		});
		buttons.addMember(deleButton);

		Button createButton = new Button("Новий гравець");
		createButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				editForm.clearValues();
			}
		});
		buttons.addMember(createButton);

		return buttons;
	}

	private VLayout createRightPanel() {
		VLayout leftPanel = new VLayout(10);
		leftPanel.setWidth("*");
		leftPanel.setHeight100();
		return leftPanel;
	}

	private HLayout createMainLayout() {
		HLayout mainLayout = new HLayout(10);
		mainLayout.setWidth100();
		mainLayout.setHeight("*");
		return mainLayout;
	}

}
